// pages/andon/tag_unbind/tag_unbind.js
const behavior = getApp().require("behaviors/requests.js")
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [behavior],
  data: {
    postPicker: false,
    factoryPicker: false,
    linePicker: false,
    lines: [],
    factory: "",
    line: "",
    material_name: "",
    tag_id_line: "",
    tag_id_person: "",
    tag_id_car: "",
    led_location: "",
    all_lines: [],
    factories: ["西区家用总装", "东区家用总装", "商用总装", "中山基地"],
    post: [],
  },

  showPostPicker() {
    this.setData({
      postPicker: true
    })
  },

  showFactoryPicker() {
    this.setData({
      factoryPicker: true
    })
  },

  showLinePicker() {
    this.setData({
      linePicker: true
    })
  },

  onConfirmFactory(event) {
    const { value, index } = event.detail;
    const line = this.data.all_lines[index];
    this.setData({
      lines: line,
      "factory": value,
      factoryPicker: false
    });
  },

  onConfirmLine(event) {
    const that = this
    const value = event.detail.value
    this.setData({
      line: value,
      linePicker: false
    })
    const url = 'https://logisticsengineering.gree.com/andon_system/get_material_list';
    const data = {
      "location": that.data.line
    }
    this.get(url, data).then((res) => {
      console.log(res)
      const code = res.code
      if (code == 20000) {
        that.setData({
          post: res.data
        })
      }
      else {
        wx.showToast({
          title: res.msg,
        })
      }
    }).catch((err) => {
      console.log(err)
    })
  },

  onConfirmPost(event) {
    const that = this
    const value = event.detail.value
    this.setData({
      material_name: value,
      postPicker: false
    })
    const url = 'https://logisticsengineering.gree.com/andon_system/material_name_To_tagid';
    const data = {
      "location": that.data.line,
      "material_name": value
    };
    this.get(url, data).then((res) => {
      wx.showToast({
        title: res.msg,
      })
      const data = res.data
      that.setData({
        "tag_id_line": data.line_id,
        "tag_id_car": data.car_id,
        "tag_id_person": data.person_id,
        "led_location": data.led_location
      })
    }).catch((err) => {
      console.log(err)
    })
  },
  cancel_factoryPicker() {
    this.setData({
      factoryPicker: false
    })
  },
  cancel_linePicker() {
    this.setData({
      linePicker: false
    })
  },

  cancel_postPicker() {
    this.setData({
      postPicker: false
    })
  },

  getLines() {
    const that = this;
    const url1 = 'https://logisticsengineering.gree.com/andon_system/get_west_line_list';
    const url2 = 'https://logisticsengineering.gree.com/andon_system/get_east_line_list';
    const url3 = 'https://logisticsengineering.gree.com/andon_system/get_ShangY_line_list';
    const url4 = 'https://logisticsengineering.gree.com/andon_system/get_ZhongS_line_list';
    this.get(url1).then((res) => {
      that.setData({
        'all_lines[0]': res.data
      })
    }).catch((err) => {
      console.log(err)
    })
    this.get(url2).then((res) => {
      that.setData({
        'all_lines[1]': res.data
      })
    }).catch((err) => {
      console.log(err)
    })
    this.get(url3).then((res) => {
      that.setData({
        'all_lines[2]': res.data
      })
    }).catch((err) => {
      console.log(err)
    })
    this.get(url4).then((res) => {
      that.setData({
        'all_lines[3]': res.data
      })
    }).catch((err) => {
      console.log(err)
    })
  },

  onSubmit() {
    const that = this;
    const url = 'https://logisticsengineering.gree.com/andon_system/del_binding';
    const data = {
      data: {
        factory: that.data.factory,
        line: that.data.line,
        material_name: that.data.material_name,
        tag_id_line: that.data.tag_id_line,
        tag_id_person: that.data.tag_id_person,
        tag_id_car: that.data.tag_id_car,
        led_location: that.data.led_location
      }
    };
    this.get(url, data).then((res) => {
      console.log(res)
        wx.showToast({
          title: res.msg,
        })
    }).catch((err) => {
      console.log(err)
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getLines();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})